#!/usr/bin/env python
# coding=utf-8
from __future__ import absolute_import

import tornado.web

from CommonLib import *
from database.models.Controller import Controller
from database.models.ControllerPoint import ControllerPoint
from database.models.SlaveID import SlaveID
from database.models.Normal import Normal
from database.models.OperationSet import OperationSet
from database.models.TimingSwitch import TimingSwitch
from database.models.DatabaseCommon import *
import WrapCursor

class ResetMasterInfoHandler(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        r = WrapCursor.WrapCursor()
        try:
            ojson = json.loads(self.request.body)
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except Exception as e:
            self.write(r.Error('request data error:' + str(e.message)))
        sqls = ["delete from ControlPoints ;",
                "delete from Controller;",
                "delete from slaveIDs;",
                "delete from NormalStep;",
                "delete from OptSet;",
                "delete from TimingSwitch;",
                ]
        s = Session()
        c = s.query(Controller).all()
        for i in c:
            s.delete(i)
        for i in s.query(ControllerPoint).all():
            s.delete(i)
        for i in s.query(SlaveID).all():
            s.delete(i)
        for i in s.query(Normal).all():
            s.delete(i)
        for i in s.query(OperationSet).all():
            s.delete(i)
        for i in s.query(TimingSwitch).all():
            s.delete(i)

        try:
            s.commit()
        except:
            self.write(r.Error(u'错误:保存错误'))
        self.write(r.Ok(u'成功'))
